package service;

import gui.IResource_Class;
import gui.IStuff;
import gui.PublicMethod;

import java.sql.Date;
import java.util.List;

import dao.DaoImp_Class;
import dao.IDao_Class;

import model.Class_NIIT;
import model.Classroom;
import model.CommonList;
import model.Major;
import model.Student;
import model.Teacher;
import model.Type;

public class ServiceImp_Class implements IService_Class,IResource_Class
{
	private IDao_Class dao_Class;
	private CommonList common;

	public ServiceImp_Class(IDao_Class dao_Class)
	{
		common = CommonList.getInstance();
		this.dao_Class = dao_Class;
	}

	public List<Class_NIIT> queryAll(int scale)
	{
		return dao_Class.queryAll(scale);
	}

	public List<Class_NIIT> queryByClassRoom(String classroom, int scale)
	{
		return dao_Class.queryByClassRoom(classroom, scale);
	}

	public List<Class_NIIT> queryByDate(String date, int scale)
	{
		return dao_Class.queryByDate(date, scale);
	}

	public List<Class_NIIT> queryByMajor(String major, int scale)
	{
		return dao_Class.queryByMajor(major, scale);
	}

	public List<Class_NIIT> queryByTeacher(String teacher, int scale)
	{
		return dao_Class.queryByTeacher(teacher, scale);
	}

	public List<Class_NIIT> queryByType(String type, int scale)
	{
		return dao_Class.queryByType(type, scale);
	}

	public List<Class_NIIT> queryByDescribe(String describe, int scale)
	{
		return dao_Class.queryDescribe(describe, scale);
	}

	public List<Class_NIIT> queryByCurrentStudentCount(int currentStudentCount,
			int scale)
	{
		return dao_Class.queryByCurrentStudentCount(currentStudentCount, scale);
	}

	public int addTeacher(Teacher teacher)
	{
		if (common.getTeacher().containsKey(teacher.getName()))
		{
			return -1;
		}
		else
		{
			int result = dao_Class.addTeacher(teacher);
			if (result == 1)
			{
				common.setTeacherFresh(false);
			}
			return result;
		}
	}

	public int addClassroom(Classroom classroom)
	{
		if (common.getClassroom().containsKey(classroom.getName()))
		{
			return itemAlreadyExist;
		}
		else
		{
			int result = dao_Class.addClassroom(classroom);
			if (result == success)
			{
				common.setClassroomFresh(false);
			}
			return result;
		}
	}

	public int addMajor(Major major)
	{
		if (common.getMajor().containsKey(major.getName()))
		{
			return itemAlreadyExist;
		}
		else
		{
			int result = dao_Class.addMajor(major);
			if (result == success)
			{
				common.setMajorFresh(false);
			}
			return result;
		}
	}

	public int addType(Type type)
	{
		if (common.getType().containsKey(type.getName()))
		{
			return itemAlreadyExist;
		}
		else
		{
			int result = dao_Class.addType(type);
			if (result == success)
			{
				common.setTypeFresh(false);
			}
			return result;
		}
	}

	public int deleteClassroom(Classroom classroom)
	{
		if (!common.getClassroom().containsKey(classroom.getName()))
		{
			return impossibleToDeleteSthNotExist;
		}
		else
		{
			int result = dao_Class.deleteClassroom(classroom);
			if (result == success)
			{
				common.setClassroomFresh(false);
			}
			return result;
		}
	}

	public int deleteMajor(Major major)
	{
		if (!common.getMajor().containsKey(major.getName()))
		{
			return impossibleToDeleteSthNotExist;
		}
		else
		{
			int result = dao_Class.deleteMajor(major);
			if (result == success)
			{
				common.setMajorFresh(false);
			}
			return result;
		}
	}

	public int deleteTeacher(Teacher teacher)
	{
		if (!common.getTeacher().containsKey(teacher.getName()))
		{
			return impossibleToDeleteSthNotExist;
		}
		else
		{
			int result = dao_Class.deleteTeacher(teacher);
			if (result == success)
			{
				common.setTeacherFresh(false);
			}
			return result;
		}
	}

	public int deleteType(Type type)
	{
		if (!common.getType().containsKey(type.getName()))
		{
			return -impossibleToDeleteSthNotExist;
		}
		else
		{
			int result = dao_Class.deleteType(type);
			if (result == success)
			{
				common.setTypeFresh(false);
			}
			return result;
		}
	}

	public int addClass(Class_NIIT classNIIT)
	{
		boolean isUsing = common.getClassroom_class().containsKey(classNIIT.getClassroom());
		if (isUsing)
		{
			return classroomAlreadyTaken;
		}
		else
		{
			int result = dao_Class.addClass(classNIIT);
			if (result == success)
			{
				common.setClassroom_classFresh(false);
			}
			return result;
		}
	}

	public void modifyDescription(Class_NIIT classNIIT, String description)
	{
		dao_Class.modifyDescription(classNIIT, description);
	}

	public int deleteClass(Class_NIIT class_NIIT)
	{
		if (class_NIIT.getPeople() > 0)
		{
			return studentMoreThanZero;
		}
		else
		{
			int result = dao_Class.deleteClass(class_NIIT);
			if (result == success)
			{
				common.setClassroom_classFresh(false);
			}
			return result;
		}
	}

	public int modifyClass(Class_NIIT originalClass, Class_NIIT modifyClass)
	{
		if (originalClass.equals(modifyClass))
		{
			return noChange;
		}
		else if (!originalClass.getClassroom().equals(
				modifyClass.getClassroom()))
		{
			if (!common.getClassroom().containsKey(modifyClass.getClassroom()))
			{
				return noSuchClassroom;
			}
			else
			{
				List<Class_NIIT> classList = queryByClassRoom(modifyClass
						.getClassroom(), PublicMethod.in);
				if (classList.size() > 0)
				{
					return classroomAlreadyTaken;
				}
				else
				{
					dao_Class.modifyClass(modifyClass);
					return success;
				}
			}
		}
		else
		{
			dao_Class.modifyClass(modifyClass);
			return success;
		}
	}

	public int combineClass(Class_NIIT modifyClass,
			IService_Student serviceStudent)
	{
		String classroom = modifyClass.getClassroom();
		int classId = common.getClassroom_class().get(classroom);
		List<Student> students = serviceStudent.queryByClassroom(classroom,
				PublicMethod.in);
		for (int i = 0; i < students.size(); i++)
		{
			int r = dao_Class.modifyClassId(students.get(i), classId);
			if (r != success)
			{
				return r;
			}
		}
		int result = deleteClass(modifyClass);
		if (result == success)
		{
			common.setClassroom_classFresh(false);
		}
		return result;
	}

	public int endClass(Class_NIIT class_NIIT)
	{
		int result=dao_Class.endClass(class_NIIT);
		if (result==success)
		{
			common.setClassroom_classFresh(false);
		}
		return result;
	}

}
